package com.scs.application.modules.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.model.TreeModel;
import com.scs.application.core.model.TreeNode;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.upms.consts.DictConsts;
import com.scs.application.modules.upms.entity.Menu;
import com.scs.application.modules.upms.model.MenuTreeModel;
import com.scs.application.modules.upms.request.MenuQueryRequest;
import com.scs.application.modules.upms.security.UserPrincipal;
import com.scs.application.modules.upms.service.MenuService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 */
@RestController
@RequestMapping("/upms/menus")
public class MenuController extends CrudController<Menu, String, MenuService> implements ImAndExAbleController<MenuQueryRequest> {


    @GetMapping("/page")
    public PageResult page(MenuQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(MenuQueryRequest request) {
        List<Menu> list = service.list(new QueryWrapper<Menu>()
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName()).orderByAsc("sort")
                .notIn(StringUtils.isNotBlank(request.getExcludeType()), "type", StringUtils.defaultString(request.getExcludeType()).split(","))
        );
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/tree")
    public CommonResult tree(MenuQueryRequest request) {

//        List<Menu> list = service.list(new QueryWrapper<Menu>()
//                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
//                .notIn(StringUtils.isNotBlank(request.getExcludeType()), "type", StringUtils.defaultString(request.getExcludeType()).split(","))
//        );
        String userId = UserUtils.currentUser().getId();
        List<Menu> list = service.getUserMenu(userId, DictConsts.MENU_TYPE_BUTTON.equalsIgnoreCase(request.getExcludeType()));
        TreeNode root = null;
        if (request.isAutoAddRoot()) {
            root = new TreeNode();
            root.setId("0");
            root.setTitle("菜单管理");
        }
        TreeModel<Menu> treeModel = new MenuTreeModel(list, "name", root);
        return CommonResult.ok("查询成功", treeModel.asList());
    }

    @GetMapping("/listForRole")
    public CommonResult listForRole(MenuQueryRequest request) {

//        List<Menu> list = service.list(new QueryWrapper<Menu>()
//                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
//                .notIn(StringUtils.isNotBlank(request.getExcludeType()), "type", StringUtils.defaultString(request.getExcludeType()).split(","))
//        );
        String userId = UserUtils.currentUser().getId();
        List<Menu> list = service.getUserMenu(userId, DictConsts.MENU_TYPE_BUTTON.equalsIgnoreCase(request.getExcludeType()));
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/navMenu")
    public CommonResult navMenu(MenuQueryRequest request) {
        //根据用户权限进行菜单筛选
        UserPrincipal user = UserUtils.currentUser();;
        List<Menu> list = service.getUserMenu(user.getId(), true);

        return CommonResult.ok("查询成功", navMenuParse(list));
    }

    @GetMapping("/navMenuList")
    public CommonResult navMenuList(MenuQueryRequest request) {
        //根据用户权限进行菜单筛选
        UserPrincipal user = UserUtils.currentUser();;
        List<Menu> list = service.getUserMenu(user.getId(), true);

        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/appMenu")
    public CommonResult appMenu(MenuQueryRequest request) {
        //根据用户权限进行菜单筛选
        UserPrincipal user = UserUtils.currentUser();;

        List<Menu> list = service.getUserAppMenu(user.getId(), true);

        return CommonResult.ok("查询成功", navMenuParse(list));
    }

    private List<TreeNode> navMenuParse(List<Menu> list) {
        TreeModel<Menu> treeModel = new MenuTreeModel(list, "name");
        return treeModel.asList();
    }


}
